package com.wrs.androidkeepalive

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.content.IntentFilter
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.widget.Button
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.util.SPUtils
import com.wrs.androidkeepalive.broadcastreceiver.BootCompleteReceiver
import com.wrs.androidkeepalive.service.ForegroundService
import com.wrs.androidkeepalive.service.KeepAliveJobService
import com.wrs.androidkeepalive.service.LocalForegroundService
import com.wrs.androidkeepalive.service.MusicService
import com.wrs.androidkeepalive.service.MyAccessibilityService
import com.wrs.androidkeepalive.service.RemoteForegroundService
import com.wrs.androidkeepalive.utils.ScreenUtils
import java.util.Timer
import java.util.TimerTask


class MainActivity : ComponentActivity() {
    var tag = "keepalive MainActivity"
    var timer: Timer? = null;
    var count: Int = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.noZhangaiBtn).setOnClickListener {
            noZhangaiBtnClick()
        }
//        findViewById<Button>(R.id.inComeBtn).setOnClickListener {
//            inComeBtnClick()
//        }
        findViewById<Button>(R.id.settingBtn).setOnClickListener {
            settingBtnClick()
        }
        findViewById<Button>(R.id.serviceBtn).setOnClickListener {
            serviceBtnClick()
        }

       requestPermission()

        // 前台服务
        val intent = Intent(this, ForegroundService::class.java)
        intent.setFlags(FLAG_ACTIVITY_NEW_TASK)
         startService(intent)

        val musicIntent = Intent(this, MusicService::class.java)
        musicIntent.setFlags(FLAG_ACTIVITY_NEW_TASK)
        startService(musicIntent)

        // 通过前台 Service 提升应用权限
        // 启动普通 Service , 但是在该 Service 的 onCreate 方法中执行了 startForeground
        // 变成了前台 Service 服务
        startService( Intent(this, LocalForegroundService::class.java));
        startService( Intent(this, RemoteForegroundService::class.java))

        // JobScheduler 拉活
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            KeepAliveJobService.startJob(this);
        }



        // 监听锁屏和解锁通知，不能静态注册广播，只能动态注册
        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_SCREEN_ON)
        filter.addAction(Intent.ACTION_SCREEN_OFF)
        registerReceiver(BootCompleteReceiver(), filter)

        this.timer = Timer()
        this.timer?.schedule(object : TimerTask() {
            override fun run() {
                count = count + 1
                Log.e(tag, "count:${count}")
                ScreenUtils.isScreenLock(applicationContext, tag)
            }
        }, 0, 2000)

        "".toLowerCase().contains("")
    }

    fun startServicess(context: Context, cls: Class<*>?) {
        context.startService(Intent(context, cls))
    }

    fun  requestPermission() {
        val permissions = mutableListOf<String>()
        permissions.add(Manifest.permission.FOREGROUND_SERVICE)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions.add(Manifest.permission.POST_NOTIFICATIONS)
        }
        ActivityCompat.requestPermissions(this, permissions.toTypedArray(), 0)
    }

    fun  serviceBtnClick(){
        // 无障碍服务
        val myAccessibilityServiceIntent = Intent(this, MyAccessibilityService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(myAccessibilityServiceIntent);
        } else {
            startService(myAccessibilityServiceIntent);
        }
    }

    fun  settingBtnClick() {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        val uri = Uri.fromParts("package", packageName, null)
        intent.setData(uri)
        startActivity(intent)
    }

    fun  inComeBtnClick() {
SPUtils.getInstance().remove("")
        SPUtils.getInstance().getString("")
        SPUtils.getInstance().put("", "")
    }

    fun noZhangaiBtnClick() {
        val accessIntent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
        startActivity(accessIntent)
    }
}